﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using models;
using System.Globalization;
using System.IO;

namespace foulekModelTester
{
    class Program
    {
        static void Main(string[] args)
        {

            FoulekModelContainer context = new FoulekModelContainer();

            IEnumerable<DirectoryInfo> directories = Directory.GetDirectories(@"E:\Logos").ToList().Select(directory => new DirectoryInfo(directory));


            if (!context.Languages.Any())
            {
                var cultureInfo = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures).ToList();
                cultureInfo.ForEach(culture => context.Languages.Add(new Language() { CultureName = culture.TextInfo.CultureName, DisplayName = culture.DisplayName, NativeName = culture.NativeName }));
                context.SaveChanges();
            }

            if (!context.Categories.Any())
            {
                directories.ToList().ForEach(directory => context.Categories.Add(new Category() { Name = directory.Name }));
                context.SaveChanges();
            }





            foreach (var directory in directories)
            {
                var currentCategory = context.Categories.Where(category => category.Name == directory.Name).Single();

                var recursives = directory.GetFiles();

                foreach (var file in recursives)
                {
                    //var path = Path.GetDirectoryName(file.FullName);
                    //File.Move(file.FullName, Path.Combine(path, file.Name.Replace("-logo-primary", "").Replace("-", " ")));

                    //if (!context.Questions.Any(question=>question.Name==file.Name.Replace(".jpg", "") && question.Category==currentCategory))
                    //{

                    context.Questions.Add(new Logo() { Category = currentCategory, Name = file.Name.Replace(".jpg", ""), URL = string.Format("/Images/Logos/{0}/{1}", currentCategory.Name, file.Name), Weight = 0, Answer = file.Name.Replace(".jpg", "") });

                    //}


                }
                context.SaveChanges();
            }







            if (!context.Users.Any(u => u.Email == "yoann@almaksoft.com"))
            {
                context.Users.Add(new User()
                  {
                      Birthday = new DateTime(1984, 01, 16),
                      Email = "yoann@almaksoft.com",
                      FirstName = "Yoann",
                      LastName = "ABDEL AAL",
                      IsValidated = false,
                      Status = UserStatus.Registered,
                      Language = context.Languages.Where(l => l.DisplayName.ToLower().Contains("french")).First()
                  });
                context.SaveChanges();
            }
            var user = context.Users.Find(1);

            if (!context.Games.Any(g => g.Name == "First Logo Game"))
            {
                context.Games.Add(new Game()
                 {
                     Name = "First Logo Game",
                     Description = "First logo game created for testing purpose",
                     Logo = "http://pressstartbutton.fr/images2/game_logo.jpg",
                     Type = GameType.Logo
                 });
                context.SaveChanges();
            }
            var game = context.Games.Find(1);

            if (!context.Rules.Any(rule => rule.Name == "Default Rule"))
            {
                context.Rules.Add(new FoulekRule()
                {
                    Name = "Default Rule",
                    RoundTime = new TimeSpan(0, 0, 1, 0, 0),
                    Winners = 20,
                    Commission = 10
                });
                context.SaveChanges();
            }
            var foulekRule = context.Rules.Find(1);


            if (!game.Rules.Any())
            {
                game.Rules.Add(foulekRule);
                context.SaveChanges();
            }

            if (!game.Rooms.Any())
            {
                context.Rooms.Add(new Room()
                {
                    Name = "First Logo Game Room",
                    Game = game,
                    Buyin = 1,
                    CreationDate = DateTime.Now,
                    MaxPlayers = 64,
                    Status = RoomStatus.InQueue
                });
                context.SaveChanges();
            }

            var room = context.Rooms.Find(1);

            if (!user.Rooms.Any(u => u.Room == room))
            {
                user.Rooms.Add(new UserInRoom()
                {
                    JoinedDate = DateTime.Now,
                    Room = room,
                });
                context.SaveChanges();
            }
            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);

            foreach (var culture in cultures)
            {

            }
        }
    }
}
